package org.edu.modules.growth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.edu.modules.growth.config.QueryConditionTool;
import org.edu.modules.growth.entity.VeJsczWorkCount;
import org.edu.modules.growth.entity.bo.VeBaseSemBO;
import org.edu.modules.growth.mapper.IVeJsczWorkCountMapper;
import org.edu.modules.growth.service.IVeBaseManageService;
import org.edu.modules.growth.service.IVeJsczWorkCountService;
import org.edu.modules.growth.vo.QueryCondition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class VeJsczWorkCountServiceImpl extends ServiceImpl<IVeJsczWorkCountMapper,
        VeJsczWorkCount> implements IVeJsczWorkCountService {

    private IVeJsczWorkCountMapper mIVeJsczWorkCountMapper;

    private IVeBaseManageService mBaseManageService;

    @Override
    public Map<String,Object> selectPage(QueryCondition queryCondition ){
        HashMap<String,Object> map = new HashMap<>();
        IPage<VeJsczWorkCount> page = new Page<>(queryCondition.getCurrent(), queryCondition.getPageSize());
        //创建查询的条件
        QueryWrapper<VeJsczWorkCount> wrapper = new QueryWrapper<>() ;
        QueryConditionTool.advancedQueryPage(queryCondition.getConditions(),wrapper);
        wrapper.orderByDesc("semId");
        page = mIVeJsczWorkCountMapper.selectPage(page,wrapper);
        //获取数据
        List<VeJsczWorkCount> list = page.getRecords();
        //统计页码
        Integer count = mIVeJsczWorkCountMapper.selectCount(wrapper);
        map.put("list",list);
        map.put("count", count);
        return map;
    }

    @Override
    public Map<String, Object> editById(VeJsczWorkCount jsczWorkCount) {
        Map<String, Object> map = Maps.newHashMap() ;
        map.put("result",false) ;
        // 查询非改id下的教师是否有学期数据
        // 查询对应工号老师、学期是否有数据
        QueryWrapper<VeJsczWorkCount> wrapper = new QueryWrapper<>() ;
        wrapper.eq("GH",jsczWorkCount.getGH()) ;
        wrapper.eq("semId",jsczWorkCount.getSemId()) ;
        wrapper.ne("id",jsczWorkCount.getId()) ;
        List<VeJsczWorkCount> list =  mIVeJsczWorkCountMapper.selectList(wrapper) ;
        if( !CollectionUtils.isEmpty(list)){
            map.put("msg","抱歉，该学期的教师工作量已记录在另一条数据中，本条记录无法修改！") ;
        }
        mIVeJsczWorkCountMapper.updateById(jsczWorkCount) ;
        map.put("result",true) ;
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void excelImport(List<VeJsczWorkCount> list) throws Exception {
        List<VeBaseSemBO> b =  mBaseManageService.semesterVo().getResult() ;
        Map<String,List<VeBaseSemBO>> semMap = b.stream().collect(Collectors.groupingBy(VeBaseSemBO::getXqmc)) ;

        Map<String,Object> map = Maps.newHashMap() ;
        map.put("result",false) ;
        Long time = System.currentTimeMillis()/1000;
//                LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        for ( VeJsczWorkCount ve : list) {
            //校验学期信息
            List<VeBaseSemBO> semList = semMap.get(ve.getSemName()) ;
            if( CollectionUtils.isEmpty(semList) ){
                throw  new Exception(ve.getSemName()+"学期名称无效！") ;
            }
            VeBaseSemBO sem = semList.get(0) ;
            // TODO 校验教师信息

            // 查询对应工号老师、学期是否有数据
            QueryWrapper<VeJsczWorkCount> wrapper = new QueryWrapper<>() ;
            wrapper.eq("GH",ve.getGH()) ;
            wrapper.eq("semId",sem.getId()) ;
            List<VeJsczWorkCount> workloads =  mIVeJsczWorkCountMapper.selectList(wrapper) ;
            // 若存在同学期工作量，数据覆盖
            if( !com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(workloads) ){
                ve.setId(workloads.get(0).getId()) ;
                mIVeJsczWorkCountMapper.updateById(ve) ;
            }else{
                ve.setAddTime(time).setTerminalId(1L) ;
//                .setAddUserId(Long.parseLong(user.getId()))
                mIVeJsczWorkCountMapper.insert(ve) ;
            }
        }
    }


}
